#ifndef SSP_HANDLE_H
#define SSP_HANDLE_H

#include <stdint.h>
#include <functional>
#include <string>
#include <atomic>
#include <ImageType.hpp>
#include <memory>
#include "h264decode_gst.hpp"
#include "imf/ssp/sspclient.h"
#include <thread>
#include <stop_token>
#include <pthread.h>
#include "h264Save_gst.hpp"

class sspHandle
//这个类用于处理SSP的回调函数
{
  public:
    sspHandle(imf::SspClient* clientPtr);
    virtual ~sspHandle();
    void SetNewImageCallback(std::function<void(ImageType)> newImage);
    void SetDisconnectEvent(std::function<void()>);
    void Start();
    void Stop();
    void StartRecord(const std::string & filePath);
    void StopRecord();
  protected:
    std::unique_ptr<h264decode_gst> decode;
    std::unique_ptr<H264Save> saver;
    unsigned imgHeight=1080,imgWidth=1920;
    friend class SSP;
    void H264DataCallback(struct imf::SspH264Data * h264);
    void AudioDataCallback(struct imf::SspAudioData * data);
    void MetaDataCallback(struct imf::SspVideoMeta * v,struct imf::SspAudioMeta * a,struct imf::SspMeta * m);
    void DisconnectCallback();
  private:
    std::jthread workThread;
    void threadFunc_dataReceive(std::stop_token stoken);
    pthread_t threadID;

    long lastFrameID=0;  //用来判断是否要重置解码器
    std::function<void(ImageType)> funcNewImageEvent=[](ImageType){};
    std::function<void()> DisconnectEvent=[&]{};
    imf::SspClient* clientPtr;
};

#endif
